package com.esp.soccertutor.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.net.ConnectivityManager;

public class WebInterface {
	
	public static String TAG = WebInterface.class.getCanonicalName();
	
	public static boolean isOnline() {
		
		try {
		    ConnectivityManager cm = (ConnectivityManager) Const.CONTEXT
			    .getSystemService(Context.CONNECTIVITY_SERVICE);
	
		    if (cm != null) {
			return cm.getActiveNetworkInfo().isConnectedOrConnecting();
		    } else {
			return false;
		    }
		} catch (Exception e) {
		    return false;
		}
	}
	
	public static String doPost(String url, ArrayList<BasicNameValuePair> params) throws Exception{
		
		String strXML = null;
		HttpEntity httpentity = null;
		
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);
	
		httppost.setEntity(new UrlEncodedFormEntity(params));
		HttpResponse response = httpclient.execute(httppost);
	
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
	
			httpentity = response.getEntity();
			strXML = new String(EntityUtils.toString(httpentity));
		}
	
		//release
		httpentity = null;
		response = null;
		httppost = null;
		httpclient = null;
		
		return strXML;
	}	
	

	public static String doGet(String strURL) throws Exception {
		HttpEntity httpentity = null;
		HttpGet httpGet = null;
		String strResponse = null;
		long startTime = System.currentTimeMillis();
		
		HttpParams httpParameters = new BasicHttpParams();
		// Set the timeout in milliseconds until a connection is established.
		int timeoutConnection = 90000;
		HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
		// Set the default socket timeout (SO_TIMEOUT) 
		// in milliseconds which is the timeout for waiting for data.
		int timeoutSocket = 90000;
		HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
		
		HttpClient httpclient = new DefaultHttpClient(httpParameters);
		
		strURL = strURL.replace(" ", "%20");
		
		Log.print(WebInterface.class + "::doGet()", "URL : " + strURL.replace(" ", "%20"));
		
		httpGet = new HttpGet(strURL);
		
		HttpResponse response = httpclient.execute(httpGet);
		 
		if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
			httpentity = response.getEntity();
			strResponse = new String(EntityUtils.toString(httpentity));
			
	    }
				
		Log.print(WebInterface.class + "::doGet()", "Response : " + strResponse);
		Log.debug("doGet() :: Execution Time :: ", (System.currentTimeMillis() - startTime)/1000+"");
		
		//Release
		response = null;
		httpclient = null;
		httpGet = null;
		httpentity = null;
		
		return strResponse;
	}		
	
	public static String readStream(InputStream is) throws IOException {
		int ch = 0;
		String str = new String();
	
		while ((ch = is.read()) != -1) {
			str += (char) ch;
		}
	
		is.close();
	
		return str;
	}
	
	public static InputStream download(String paraURL) throws IOException 
	{
		URL url = null;
		HttpURLConnection conn = null;

		url = new URL(paraURL);
		conn = (HttpURLConnection) url.openConnection();

		if ((conn).getResponseCode() == HttpURLConnection.HTTP_OK)
			return conn.getInputStream();
		else
			return null;
	}
	
	
	
}
